package org.hot100_2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class leetcode56 {
    public int [][] merge(int [] []intervals){

        Arrays.sort(intervals, (Arrays1, Arrays2) -> Arrays1[0]-Arrays2[0]);
        List<int[]> list =new ArrayList<>();

        int left = intervals[0][0];
        int right = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0]<=right){
                right=Math.max(right,intervals[i][1]);
            }else {
                int [] temp = {left,right};
                list.add(temp);
                left=intervals[i][0];
                right=intervals[i][1];
            }
        }

        int [] temp = {left,right};
        list.add(temp);

        int [][] res = new int[list.size()][2];

        for (int i = 0; i < res.length; i++) {
            res[i]=list.get(i);
        }
        return res;
    }
}
